from models import User
from django.template import Context, loader
from django.http import HttpResponse
from django.shortcuts import render
import hashlib


class Login(object):

	"""docstring for Login"""
	def __init__(self, request):
		self.request = request
		


	def login(self, username, password):
		if self.isLogged():
			return True;
		password= hashlib.sha1(password).hexdigest();
		if len(User.objects.filter(username=username))<1:
			return False;
		self.user = User.objects.get(username=username)
		if self.user.username != None:
			if self.user.password == password:
				if self.user.privilage!=0:
					return False;
				self.request.session['logged'] = self.user.username
				return True;
		return False;
	
	
	def logout(self):
		if self.isLogged():
			del self.request.session['logged']
		
	def isLogged(self):
		if self.request.session.get('logged') is not None:
			self.user = User.objects.get(username=self.request.session['logged'])
			return True
		return False
	def isAdministrator(self):
		if self.request.session.get('logged') is not None:
			self.user = User.objects.get(username=self.request.session['logged'])
			if user.privilage ==0:
				return True
			
		return False

		

	
	